<?php

namespace app\web\controller;
use app\web\model\OrderItemsModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 销量统计
 */
class SalesEnterController extends StoreBaseController
{
 	protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new OrderItemsModel();
    }
    /**
     * 销量统计
     * @author [lx] 
     */
    public function index( $page = 1){
    	$isline = request()->param('isline');
    	$agent_id = session('storeInfo')['id'];
		if($isline==0){
			//优惠购销量统计
        for($i=0;$i<7;$i++){
        	$time=date('Y-m-d',strtotime(-$i.'day'));
        	$day[$i]=substr($time,5,5);
       		$where['add_time']=['like','%'.$time.'%'];
      		$ysums[$i] = Db::name("mall_order_item")
      		->order('add_time ASC')
      		->where('agent_id',$agent_id)
      		->where('goods_lanmu','2')
      		->where($where)->sum('number');
      		$ysum=array_sum($ysums);
		}
		//疯狂抢销量统计
		for($i=0;$i<7;$i++){
        	$time=date('Y-m-d',strtotime(-$i.'day'));
       		$where['add_time']=['like','%'.$time.'%'];
      		$fsums[$i] = Db::name("mall_order_item")->order('add_time ASC')->where('agent_id',$agent_id)
      		->field('add_time')
      		->where('goods_lanmu','1')
      		->where($where)->sum('number');
      		$fsum=array_sum($fsums);
		}
		//赚积分销量统计
		for($i=0;$i<7;$i++){
        	$time=date('Y-m-d',strtotime(-$i.'day'));
       		$where['add_time']=['like','%'.$time.'%'];
      		$zsums[$i] = Db::name("mall_order_item")->order('add_time ASC')->where('agent_id',$agent_id)
      		->field('add_time')
      		->where('goods_lanmu','3')
      		->where($where)->sum('number');
      		$zsum=array_sum($zsums);
		}
		//云拼团销量统计
		for($i=0;$i<7;$i++){
        	$time=date('Y-m-d',strtotime(-$i.'day'));
       		$where['add_time']=['like','%'.$time.'%'];
      		$psums[$i] = Db::name("mall_order_item")->order('add_time ASC')->where('agent_id',$agent_id)
      		->field('add_time')
      		->where('goods_lanmu','4')
      		->where($where)->sum('number');
      		$psum=array_sum($psums);
		}
		$isline=0;
        return $this->fetch('index',['ysum'=>$ysum,'ysums'=>$ysums,'fsum'=>$fsum,'fsums'=>$fsums,'zsum'=>$zsum,'zsums'=>$zsums,'psum'=>$psum,'psums'=>$psums,'isline'=>$isline,'day'=>$day]);
  
  }else{
		for($i=0;$i<7;$i++){
        	$time=date('Y-m-d',strtotime(-$i.'day'));
        	$day[$i]=substr($time,5,5);
       		$where['l.add_time']=['like','%'.$time.'%'];
      		$linesums[$i] = Db::name("line_order")
      		->alias('l')
			->join('line_orderitems t','l.id = t.order_id')
      		->order('t.add_time ASC')
      		->where('l.agent_id',$agent_id)
      		->where($where)->sum('num');
      		$linesum=array_sum($linesums);
		}
		$isline=1;
        return $this->fetch('index',['linesum'=>$linesum,'linesums'=>$linesums,'isline'=>$isline,'day'=>$day]);
  
  	}
   }


   
}